What is which?
The 'which' npm package is a simple utility to locate a command in the user's path, similar to the Unix 'which' command. It is used to find the first instance of a specified executable in the system path, which can be useful for ensuring that a required command-line tool is available and for invoking system commands from Node.js scripts.
What are which's main functionalities?
Synchronous command search
This feature allows you to synchronously find the path to an executable in the system's PATH. The 'sync' method returns the path to the specified executable if it exists.
const which = require('which');
const cmdPath = which.sync('node');
console.log(`Node.js is located at: ${cmdPath}`);
Asynchronous command search
This feature allows you to asynchronously find the path to an executable in the system's PATH. The 'which' function takes a callback that receives the path to the specified executable if it exists.
const which = require('which');
which('node', function (err, cmdPath) {
if (err) throw err;
console.log(`Node.js is located at: ${cmdPath}`);
});
Throw on not found
This feature allows you to specify whether the 'which' method should throw an error if the command is not found. By default, or when 'nothrow' is set to false, it will throw an error.
const which = require('which');
try {
const cmdPath = which.sync('unknown-command', {nothrow: false});
} catch (e) {
console.error('Command not found');
}
Other packages similar to which
find-exec
The 'find-exec' package is similar to 'which' in that it helps you find the path to executables in your PATH. However, it has a different API and may offer additional options for searching.
lookpath
The 'lookpath' package is another alternative to 'which' that provides a way to check for the existence of an executable in the system's PATH. It is designed to be a simple and lightweight solution with a promise-based API.
command-exists
The 'command-exists' package is used to check if a given command-line tool is available on the system's PATH. Unlike 'which', it does not return the path to the executable but simply a boolean indicating whether the command exists.
which
Like the unix which
utility.
Finds the first instance of a specified executable in the PATH
environment variable. Does not cache the results, so hash -r
is not
needed when the PATH changes.
USAGE
const which = require('which')
const resolved = await which('node')
const resolvedOrNull = await which('node', { nothrow: true })
const resolved = which.sync('node')
const resolvedOrNull = which.sync('node', { nothrow: true })
await which('node', { path: someOtherPath, pathExt: somePathExt })
CLI USAGE
Just like the BSD which(1)
binary but using node-which
.
usage: node-which [-as] program ...
You can learn more about why the binary is node-which
and not which
here
OPTIONS
You may pass an options object as the second argument.
path
: Use instead of the PATH
environment variable.pathExt
: Use instead of the PATHEXT
environment variable.all
: Return all matches, instead of just the first one. Note that
this means the function returns an array of strings instead of a
single string.